using System;
using UnityEngine;

namespace InventorySystem
{
    [Serializable]
    public class ItemStack
    {
        [SerializeField]
        private ItemDefinition _item;  // 物品定义
        [SerializeField]
        private int _numberOfItems;  // 物品数量

        public bool IsStackable => _item.IsStackable;  // 是否可堆叠
        public ItemDefinition Item => _item;  // 物品定义
        public int NumberOfItems
        {
            get => _numberOfItems;
            set
            {
                value = value < 0 ? 0 : value;  // 确保物品数量不小于零
                _numberOfItems = IsStackable ? value : 1;   // 如果不可堆叠，将物品数量限制在 1 以内
            }
        }

        public ItemStack(ItemDefinition item, int numberOfItems)
        {
            _item = item;
            NumberOfItems = numberOfItems;
        }
    }
}
